<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <link rel="stylesheet" href="assets/mui/css/mui.min.css">
    <link rel="stylesheet" href="assets/fontAwesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="css/base.css">
    <link rel="stylesheet" href="css/reg.css">
    <script src="assets/zepto/zepto.min.js"></script>
    <script src="assets/mui/js/mui.js"></script>
    <script src="./js/common.js"></script>
    <link rel="stylesheet" href="./assets/mui/css/mui.picker.css">
    <link rel="stylesheet" href="./assets/mui/css/mui.poppicker.css">
    <script src=""></script>
    <script src="assets/mui/js/mui.picker.js"></script>
    <script src="assets/mui/js/mui.poppicker.js"></script>
    <script src="./js/city.js"></script>
    <style>
        .yzm input[type="text"] {
            width: 100px;
            float: left;
        }
        .yzm a {
            height: 40px;
            line-height: 40px;
            color: blue;
            padding-right: 10px;
        }
    </style>
</head>

<body>
    <header class="mui-bar mui-bar-nav header">
        <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left" href="javascript:history.back(-1)"></a>
        <h1 class="mui-title">添加收获地址</h1>
        <a class="mui-icon mui-icon-search mui-pull-right"></a>

    </header>
    <nav class="mui-bar mui-bar-tab footer">
        <a class="mui-tab-item" href="index.html">
            <span class="mui-icon mui-icon-home"></span>
            <span class="mui-tab-label">首页</span>
        </a>
        <a class="mui-tab-item" href="category.html">
            <span class="mui-icon mui-icon-bars"></span>
            <span class="mui-tab-label">分类</span>
        </a>
        <a class="mui-tab-item" href="cart.html">
            <span class="mui-icon fa fa-shopping-cart"></span>
            <span class="mui-tab-label">购物车</span>
        </a>
        <a class="mui-tab-item" href="user.html">
            <span class="mui-icon mui-icon-person"></span>
            <span class="mui-tab-label">会员中心</span>
        </a>
    </nav>
    <div class="mui-content">
        <form class="mui-input-group">
            <div class="mui-input-row">
                <input type="text" name="name" class="mui-input-text" placeholder="收货人姓名">
            </div>
            <div class="mui-input-row">
                <input type="text" name="mail" class="mui-input-text" placeholder="邮政编码">
            </div>
            <div class="mui-input-row">
                <input type="text" name="pro" class="mui-input-text picker" placeholder="省市区">
            </div>
            <div class="mui-input-row">
                <input type="text" name="address" class="mui-input-text" placeholder="详细地址">
            </div>
            <div class="mui-button-row">
                <button type="button" class="addadd mui-btn mui-btn-primary">确认添加</button>
            </div>
        </form>
    </div>
</body>
<script src="./assets/artTemplate/template-web.js"></script>
<script type="text/template" id="edit">
    <form class="mui-input-group">
    <div class="mui-input-row">
        <input type="text" value="{{recipients}}" name="name" class="mui-input-text" placeholder="收货人姓名">
    </div>
    <div class="mui-input-row">
        <input type="text" value="{{postCode}}" name="mail" class="mui-input-text" placeholder="邮政编码">
    </div>
    <div class="mui-input-row">
        <input type="text" value="{{address}}" name="pro" class="mui-input-text picker" placeholder="省市区">
    </div>
    <div class="mui-input-row">
        <input type="text" value="{{addressDetail}}" name="address" class="mui-input-text" placeholder="详细地址">
    </div>
    <div class="mui-button-row">
        <button type="button" class="addadd mui-btn mui-btn-primary">确认修改</button>
    </div>
</form>
</script>
<script>
    var isAdd = getParamsByUrl(location.href, "isAdd")
    // console.log(isAdd)
    if (isAdd == "0") {
        var data = JSON.parse(localStorage.edit)
        var html = template('edit', data)
        console.log(html)
        $('.mui-content').html(html)

    }
    var picker = new mui.PopPicker({
        layer: 3
    });
    picker.setData(cityData);
    $('.picker').on('tap', function () {
        picker.show(function (selectItems) {
            console.log(selectItems[0].text); //智子
            console.log(selectItems[1].text); //zz
            console.log(selectItems[2].text); //zz
            $('.picker').val(selectItems[0].text + " " + selectItems[1].text + " " + selectItems[2].text)
        })
    })
    $('.addadd').on('tap', function () {
        var address = $.trim($('input[name="pro"]').val());
        var addressDetail = $.trim($('input[name="address"]').val())
        var recipients = $.trim($('input[name="name"]').val())
        var postcode = $.trim($('input[name="mail"]').val())
        if (address == "") {
            mui.toast('请输入省市区')
            return;
        }
        if (isAdd == "0") {
            $.ajax({
                url: '/address/updateAddress',
                type: "POST",
                data: {
                    id: JSON.parse(localStorage.edit).id,
                    address: address,
                    addressDetail: addressDetail,
                    recipients: recipients,
                    postcode: postcode
                },
                success: function (res) {
                    console.log(res)
                    if (res.success) {
                        mui.toast('修改成功')
                        setTimeout(() => {
                            window.location.href = 'address.html'

                        }, 1000);
                    } else {

                    }
                }
            })
        } else {
            $.ajax({
                url: '/address/addAddress',
                type: "POST",
                data: {
                    address: address,
                    addressDetail: addressDetail,
                    recipients: recipients,
                    postcode: postcode
                },
                success: function (res) {
                    console.log(res)
                    if (res.success) {
                        mui.toast('添加成功')
                        setTimeout(() => {
                            window.location.href = 'address.html'

                        }, 1000);
                    } else {

                    }
                }
            })
        }

    })

</script>

</html>
